<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="140px"
      style="padding-right: 40px;"
    >
      <el-form-item label="模型名称" prop="name">
        <div v-if="formType === 'view'">{{ formData.name }}</div>
        <el-input v-model="formData.name" placeholder="请输入" v-else :disabled="formType === 'update'" />
      </el-form-item>
      <el-form-item label="模型平台" prop="platform">
        <div v-if="formType === 'view'">{{ getDictLabel(DICT_TYPE.MULTI_MODEL_PLATFORM, formData.platform) }}</div>
        <el-select v-model="formData.platform" placeholder="请选择" v-else :disabled="formType === 'update'" >
          <el-option
            v-for="dict in getIntDictOptionsString(DICT_TYPE.MULTI_MODEL_PLATFORM)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="model参数" prop="model">
        <div v-if="formType === 'view'">{{ formData.model }}</div>
        <el-input v-model="formData.model" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="maxTokens参数" prop="maxTokens">
        <div v-if="formType === 'view'">{{ formData.maxTokens }}</div>
        <el-input-number v-model="formData.maxTokens" placeholder="请输入数字" :controls="false" :precision="0" :min="0" style="width: 200px;" v-else />
      </el-form-item>
      <el-form-item label="temperature参数" prop="temperature">
        <div v-if="formType === 'view'">{{ formData.temperature }}</div>
        <el-input-number v-model="formData.temperature" placeholder="请输入0~1之间的数字" :controls="false" style="width: 200px;" :min="0" :max="1" v-else />
      </el-form-item>
      <el-form-item label="topP参数" prop="topP">
        <div v-if="formType === 'view'">{{ formData.topP }}</div>
        <el-input-number v-model="formData.topP" placeholder="请输入0~1之间的数字" :controls="false" style="width: 200px;" :min="0" :max="1" v-else />
      </el-form-item>
      <el-form-item label="训练参数量" prop="paramNum">
        <div v-if="formType === 'view'">{{ formData.paramNum }}</div>
        <el-input v-model="formData.paramNum" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="token量" prop="tokenNum">
        <div v-if="formType === 'view'">{{ formData.tokenNum }}</div>
        <el-input v-model="formData.tokenNum" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="api名称" prop="apiName">
        <div v-if="formType === 'view'">{{ formData.apiName }}</div>
        <el-input v-model="formData.apiName" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="api版本" prop="apiVersion">
        <div v-if="formType === 'view'">{{ formData.apiVersion }}</div>
        <el-input v-model="formData.apiVersion" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="调用地址" prop="apiAddress">
        <div v-if="formType === 'view'">{{ formData.apiAddress }}</div>
        <el-input v-model="formData.apiAddress" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="请求方法" prop="apiMethod">
        <div v-if="formType === 'view'">{{ formData.apiMethod }}</div>
        <el-input v-model="formData.apiMethod" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="apiKey" prop="apiKey">
        <div v-if="formType === 'view'">{{ formData.apiKey }}</div>
        <el-input v-model="formData.apiKey" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="apiSecret" prop="apiSecret">
        <div v-if="formType === 'view'">{{ formData.apiSecret }}</div>
        <el-input v-model="formData.apiSecret" placeholder="请输入" v-else />
      </el-form-item>
      <el-form-item label="appId" prop="appId" v-if="formData.platform === 'SPARK'">
        <div v-if="formType === 'view'">{{ formData.appId }}</div>
        <el-input v-model="formData.appId" placeholder="请输入" v-else />
      </el-form-item>
    </el-form>
    <template #footer v-if="formType !== 'view'">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button @click="submitForm" type="primary">{{ formType === 'create' ?  '上传' : '确定'}}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { LargeModelApi, LargeModelVO } from '@/api/multiModel/largemodel'
import { getIntDictOptionsString, getDictLabel, DICT_TYPE } from '@/utils/dict'
/** 模型 表单 */
defineOptions({ name: 'LargeModelForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formType = ref('') // 表单的类型：create - 新增；update - 修改 view - 查看
const formData = ref({
  id: undefined,
  name: undefined,
  // version: undefined,
  paramNum: undefined,
  tokenNum: undefined,
  apiName: undefined,
  apiVersion: undefined,
  apiAddress: undefined,
  apiMethod: undefined,
  apiKey: undefined,
  apiSecret: undefined,
  appId: undefined,
  platform: undefined,
  model: undefined,
  maxTokens: undefined,
  temperature: undefined,
  topP:undefined,
  type: 0

})
const formRules = reactive({
  name: [{ required: true, message: '模型名称不能为空', trigger: 'blur' }],
  // version: [{ required: true, message: '模型版本不能为空', trigger: 'blur' }],
  paramNum: [{ required: true, message: '训练参数量不能为空', trigger: 'blur' }],
  tokenNum: [{ required: true, message: 'token量不能为空', trigger: 'blur' }],
  apiName: [{ required: true, message: 'api名称不能为空', trigger: 'blur' }],
  apiVersion: [{ required: true, message: 'api版本不能为空', trigger: 'blur' }],
  apiAddress: [{ required: true, message: '调用地址不能为空', trigger: 'blur' }],
  apiMethod: [{ required: true, message: '请求方法不能为空', trigger: 'blur' }],
  platform: [{ required: true, message: '模型平台不能为空', trigger: 'change' }]
  // model: [{ required: true, message: '语言模型不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, modelType:number, modelName: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type) + modelName
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    try {
      formData.value = await LargeModelApi.getLargeModel(id)
    } finally {
    }
  } else {
    formData.value.type = modelType
  }
  
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  try {
    const data = formData.value as unknown as LargeModelVO
    if (data.platform !== 'SPARK') {
      data.appId = undefined
    }
    if (formType.value === 'create') {
      await LargeModelApi.createLargeModel(data)
      message.success(t('common.createSuccess'))
    } else {
      await LargeModelApi.updateLargeModel(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    // version: undefined,
    paramNum: undefined,
    tokenNum: undefined,
    apiName: undefined,
    apiVersion: undefined,
    apiAddress: undefined,
    apiMethod: undefined,
    apiKey: undefined,
    apiSecret: undefined,
    appId: undefined,
    platform: undefined,
    model: undefined,
    maxTokens: undefined,
    temperature: undefined,
    topP:undefined,
    type: 0
  }
  formRef.value?.resetFields()
}
</script>
